package net.gzw1993.runmate.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import net.gzw1993.runmate.R;
import net.gzw1993.runmate.activity.BaseActivity;
import net.gzw1993.runmate.adapter.VPFragmentAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ChanllegeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ChanllegeFragment extends BaseFragment{
    private View view;
    @ViewInject(R.id.chanllege_viewPager)
    private ViewPager chanllege_viewPager;
    private VPFragmentAdapter mAdapter;
    private List<Fragment> fragmentList = new ArrayList<>();
    private ActionBar actionBar;

    public static ChanllegeFragment newInstance() {
        ChanllegeFragment fragment = new ChanllegeFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    public ChanllegeFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_chanllege, container, false);
        ViewUtils.inject(this,view);
        initViewPager();
        initActionBar();
        return view;
    }

    private void initViewPager() {
        fragmentList.add(LineActivityFragment.newInstance());
        fragmentList.add(SameCityFragment.newInstance());
        mAdapter = new VPFragmentAdapter(getFragmentManager(),fragmentList);
        chanllege_viewPager.setAdapter(mAdapter);
        chanllege_viewPager.addOnPageChangeListener(new PagerChecedListener());
    }

    private void initActionBar() {
        BaseActivity activity = (BaseActivity)getActivity();
        actionBar = activity.getSupportActionBar();
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);  //actionBar设置Tab式导航
        ActionBar.Tab lineAct = actionBar.newTab().setText("线上活动").setTabListener(new ViewTabListener());
        ActionBar.Tab sameCityAct = actionBar.newTab().setText("同城活动").setTabListener(new ViewTabListener());
        actionBar.addTab(lineAct);
        actionBar.addTab(sameCityAct);
    }

    /**
     * ActionBar Tab导航监听
     */
    private class ViewTabListener implements ActionBar.TabListener{

        @Override
        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
            chanllege_viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

        }

        @Override
        public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

        }
    }

    /**
     * ViewPager 滑动变化监听
     */
    private class PagerChecedListener implements ViewPager.OnPageChangeListener{

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }


    @Override
    public void onStop() {
        super.onStop();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
